package com.regent.central.bill.web.mrp.delivery;


import com.alibaba.druid.util.StringUtils;
import com.regent.central.bill.repository.mrp.dto.delivery.BillMaterialDeliveryListDTO;
import com.regent.central.bill.repository.mrp.dto.delivery.BillMaterialDeliveryPageParam;
import com.regent.central.bill.repository.mrp.dto.delivery.BillMaterialDeliveryUndoResult;
import com.regent.central.bill.repository.mrp.dto.delivery.CentralBillDeliveryCreateDTO;
import com.regent.central.bill.service.mrp.delivery.BillMaterialDeliveryService;
import com.regent.central.bill.web.mrp.BillMaterialDeliveryUndoResponse;
import com.regent.central.bill.web.mrp.delivery.response.BillMaterialDeilveryPageResponse;
import com.regent.central.bill.web.mrp.delivery.response.BillMaterialDeliveryDetailResponse;
import com.regent.central.bill.web.mrp.delivery.response.BillMaterialDeliveryListResponse;
import com.regent.central.infrastructure.web.BasicResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 送料单Controller
 *
 * @author 姚广新
 * @date 2018-06-26 14:51
 **/
@RestController
@RequestMapping(value = "/bill/material/delivery", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class BillMaterialDeliveryController {

    @Resource
    private BillMaterialDeliveryService billMaterialDeliveryService;

    //送料单列表分页接口
    @PostMapping("/page")
    public BillMaterialDeilveryPageResponse page(@RequestBody BillMaterialDeliveryPageParam param) {
        BillMaterialDeilveryPageResponse response = new BillMaterialDeilveryPageResponse();
        if(StringUtils.isEmpty(param.getUnitId())){
            response.setCode(1);
            response.setMessage("unitId不能为空");
            return response;
        }
        if(param.getPageNo()==null){
            response.setCode(1);
            response.setMessage("pageNo不能为空");
            return response;
        }
        if(param.getPageSize()==null){
            param.setPageSize(10);
        }
        response.setBillMaterialDeliveryPageDTO(billMaterialDeliveryService.findPage(param));
        return response;
    }

    //组织层送料单号搜索下拉列表接口
    @PostMapping("/querylist")
    public BillMaterialDeliveryListResponse querylist(@RequestParam("unitId") String unitId) {
        BillMaterialDeliveryListResponse response = new BillMaterialDeliveryListResponse();
        if(StringUtils.isEmpty(unitId)){
            response.setCode(1);
            response.setMessage("unitId不能为空");
            return response;
        }
        response.setList(billMaterialDeliveryService.findDeliveryOrderNosByUnitId(unitId));
        return response;
    }

    @PostMapping("/create")
    public BasicResponse create(@RequestBody CentralBillDeliveryCreateDTO createDTO) {
        billMaterialDeliveryService.createBill(createDTO);
        return new BasicResponse();
    }

    @GetMapping("/{billMaterialDeliveryId:\\w+}")
    public BillMaterialDeliveryDetailResponse detail (@PathVariable("billMaterialDeliveryId") String billMaterialDeliveryId){
        BillMaterialDeliveryDetailResponse response = new BillMaterialDeliveryDetailResponse();
        List<BillMaterialDeliveryListDTO> billMaterialDeliveryListDTOS = billMaterialDeliveryService.findDetailById(billMaterialDeliveryId);
        response.setDetailList(billMaterialDeliveryListDTOS);
        return response;
    }

    @DeleteMapping("/undo/{billMaterialDeliveryId:\\w+}")
    public BillMaterialDeliveryUndoResponse undo(@PathVariable("billMaterialDeliveryId") String billMaterialDeliveryId) {
        BillMaterialDeliveryUndoResponse response = new BillMaterialDeliveryUndoResponse();
        BillMaterialDeliveryUndoResult result = billMaterialDeliveryService.undo(billMaterialDeliveryId);
        response.setResult(result);
        return response;
    }
}
